App.directive("edCalendar", ['Page', function(Page) {
    return {

        restrict: 'A',
        link: function (scope, elem, attrs, controller, transcludeFn) {

            var Calendar=function(year,monthNum,parent){
                this.year=year;
                this.parent=parent;
                this.monthNum=monthNum-1;
                this.weekDays=["日","一","二","三","四","五","六"];
                this.nowDate=new Date;
                this.init();
            }

            Calendar.prototype={
                isLeapYear:function(y){
                    return (y>0)&&!(y%4)&&((y%100)||!(y%400));
                },
                setMonthNum:function(monthNum){
                    this.monthNum=monthNum-1;
                },
                getMonthNum:function(){
                    return this.monthNum+1;
                },
                setYearNum:function(year){
                    this.year=year;
                },
                getYearNum:function(){
                    return this.year;
                },
                init:function(){
                    var cal=document.getElementById(this.parent);
                    cal.innerHTML="";
                    this.setup(this.parent);
                },
                reflesh:function(){
                    this.setup(this.parent);
                },
                setup:function(id){
                    var date=this.nowDate;
                    var cal=document.getElementById(id);
                    // cal.innerHTML="";
                    var monthDiv=document.createElement("div");
                    var calDiv=document.createElement("div");
                    var tab=document.createElement("table");
                    monthDiv.appendChild(calDiv);
                    calDiv.innerHTML=this.getSummary();
                    monthDiv.appendChild(tab);
                    cal.appendChild(monthDiv);
                    monthDiv.className="calendar"
                    calDiv.className="title"
                    this.thead=document.createElement("thead");
                    this.tbody=document.createElement("tbody");
                    this.tfoot=document.createElement("tfoot");
                    this.tr=document.createElement("tr");
                    this.td=document.createElement("td");

                    tab.appendChild(this.thead);
                    tab.appendChild(this.tbody);
                    this.setThead();
                    this.create();
                },
                setThead:function(){
                    var day=this.weekDays;
                    var tr=this.tr.cloneNode(true);
                    this.thead.appendChild(tr);
                    for(var i=0;i<7;i++){
                        var td=this.td.cloneNode(true);
                        tr.appendChild(td);
                        td.innerHTML=day[i];
                    }
                },
                create:function(){
                    var day=new Date(this.year,this.monthNum,1);
                    var tr=this.tr.cloneNode(true);
                    var dayCount=[31,this.isLeapYear(this.year)?29:28,31,30,31,30,31,31,30,31,30,31][this.monthNum];
                    var that=this;

                    that.tbody.appendChild(tr);
                    for(var j=0;j<day.getDay();j++){// 当前月1号之前的
                        var td=that.td.cloneNode(true);
                        tr.appendChild(td);
                        td.innerHTML=" ";
                    }
                    for(var i=1;i<=dayCount;i++){
                        if((j+i)%7-1==0){
                            tr=that.tr.cloneNode(true);
                            that.tbody.appendChild(tr);
                        }
                        var td=that.td.cloneNode(true);
                        var s=i;
                        if(this.monthNum==that.nowDate.getMonth() && this.year==that.nowDate.getFullYear()){
                            if (i < that.nowDate.getDate()) {// 小于今天的不能选择
                                // td.className = 'disable';
                            } else if (i == that.nowDate.getDate()) {// 当天变红
                                s="<font color='red'>"+i+"</font>";
                            }
                        }
                        td.innerHTML=s;
                        var mon = this.monthNum+1;
                        td.title=this.year+'-'+(mon < 10 ? '0' + mon : mon)+'-'+(i < 10 ? '0' + i : i);
                        td.style.cursor="pointer";
                        td.onclick=function(){
                            var className = this.className;
                            if (className == 'disable') {

                            } else if (className == 'select') {
                                this.className = '';
                            } else {
                                this.className = 'select';
                            }
                        }
                        tr.appendChild(td);
                    }
                },
                getSummary:function(){
                    var date=this.nowDate;
                    return this.year+"年"+(this.monthNum+1)+"月";
                }
            }

            var Calendars = function() {
                this.cal = null;
                this.showYear = function(year) {
                    this.cal = new Calendar(year, 1,"calendar");
                    this.cal.init();
                    for (var i = 0; i < 11; i++) {
                        this.cal.monthNum++
                        this.cal.reflesh();
                    }
                };
                this.nextYear = function() {
                    this.showYear(this.cal.year + 1);
                };
                this.prevYear = function() {
                    this.showYear(this.cal.year - 1);
                }
                this.showHalfYear = function() {
                    this.cal=new Calendar(new Date().getFullYear(),(new Date).getMonth()+1,"calendar");
                    this.cal.init();
                    for (var i = 0; i < 5; i++) {
                        this.cal.monthNum++
                        if(this.cal.getMonthNum()>12){
                            this.cal.setMonthNum(1);
                            this.cal.year++;
                        }
                        this.cal.reflesh();
                    }
                };
                this.select = function(dates) {
                    $(dates).each(function(i, date) {
                        $(elem).find('td[title="' + date + '"]').addClass('select');
                    });
                };
                this.disable = function(dates) {
                    $(dates).each(function(i, date) {
                        $(elem).find('td[title="' + date + '"]').removeClass('select').addClass('disable');
                    });
                };
                this.selectWeekend = function() {
                    $(elem).find('tr').each(function() {
                        var ele = $(this).find('td:eq(0)');
                        if(typeof(ele.attr("title"))!="undefined") {
                            ele.addClass('select');
                        }
                        ele = $(this).find('td:eq(6)');
                        if(typeof(ele.attr("title"))!="undefined") {
                            ele.addClass('select');
                        }
                    });
                };;
                this.getEnableDates = function() {
                    var dates = [];
                    $(elem).find('tr td[title]').each(function() {
                        if (!$(this).hasClass('disable')) {
                            dates.push($(this).attr('title'));
                        }
                    });
                    return dates;
                }
                this.getSelected = function() {
                    var dates = [];
                    $(elem).find('tr td.select').each(function() {
                        dates.push($(this).attr('title'));
                    });
                    return dates;
                }
            }
            scope.calendars = new Calendars();
        }

    };
} ]);
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}